﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SqliteEFTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            GradeInfoCollection = new ObservableCollection<DBFirst.GradeInfo>();
            ClassInfoCollection = new ObservableCollection<DBFirst.ClassInfo>();
            StuInfoCollection = new ObservableCollection<DBFirst.Stu>();
            DataContext = this;
        }
        private readonly string conStr = $"datasource={System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "st.db")}";//$"datasource=F:\\sqlite\\SqliteTest\\SqliteEFTest\\st.db"; 
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));

        #region Grade
        private DBFirst.GradeInfo selGrade;
        public DBFirst.GradeInfo SelGrade { get { return selGrade; } set { selGrade = value; OnPropertyChanged("SelGrade"); } }
        public ObservableCollection<DBFirst.GradeInfo> GradeInfoCollection { get; set; }
        private void QueryGradeInfo(object sender, RoutedEventArgs e)
        {
            using (DBFirst.DbFirstEntities db = new DBFirst.DbFirstEntities())
            {
                db.Database.Connection.ConnectionString = conStr;
                QueryGradeInfo(db);
            }
        }
        private void QueryGradeInfo(DBFirst.DbFirstEntities db)
        {
            if (db.GradeInfo.Count() == 0) return;
            GradeInfoCollection.Clear();
            foreach (var item in db.GradeInfo) GradeInfoCollection.Add(item);
        }
        private void InsertGradeInfo(object sender, RoutedEventArgs e)
        {
            if (GradeInfoCollection.Last().Name == null) return;
            using (DBFirst.DbFirstEntities db = new DBFirst.DbFirstEntities())
            {
                db.Database.Connection.ConnectionString = conStr;
                var list = db.GradeInfo.ToList();
                if (list.Count() > 0 && list.Last().Name == GradeInfoCollection.Last(f => f.Name != null).Name) return;
                var addGrade = GradeInfoCollection.Last();
                db.GradeInfo.Add(new DBFirst.GradeInfo
                {
                    Director = addGrade.Director,
                    Name = addGrade.Name
                });
                db.SaveChanges();
                QueryGradeInfo(db);
            }
            MessageBox.Show("插入成功！");
        }
        private void UpdateGradeInfo(object sender, RoutedEventArgs e)
        {
            if (SelGrade == null) return;
            using (DBFirst.DbFirstEntities db = new DBFirst.DbFirstEntities())
            {
                db.Database.Connection.ConnectionString = conStr;
                var upGrade = db.GradeInfo.FirstOrDefault(f => f.GradeID == selGrade.GradeID);
                if (upGrade == null) return;
                upGrade.Name = selGrade.Name;
                upGrade.Director = selGrade.Director;
                db.SaveChanges();
                QueryGradeInfo(db);
            }
            MessageBox.Show("更新成功！");
        }
        private void DeleteGradeInfo(object sender, RoutedEventArgs e)
        {
            if (SelGrade == null) return;
            using (DBFirst.DbFirstEntities db = new DBFirst.DbFirstEntities())
            {
                db.Database.Connection.ConnectionString = conStr;
                var delGrade = db.GradeInfo.Include("ClassInfo").FirstOrDefault(f => f.GradeID == selGrade.GradeID);
                if (delGrade == null) return;
                if (delGrade.ClassInfo.Count > 0 && MessageBox.Show("该年级下有班级存在,删除后会同时删除班级信息，是否删除？", "询问", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    db.ClassInfo.RemoveRange(db.ClassInfo);
                }
                else return;
                db.GradeInfo.Remove(delGrade);
                db.SaveChanges();
                QueryGradeInfo(db);
            }
            MessageBox.Show("删除成功！");
        }
        #endregion

        #region Class
        private DBFirst.ClassInfo selClass;
        public DBFirst.ClassInfo SelClass { get { return selClass; } set { selClass = value; OnPropertyChanged("SelClass"); } }
        public ObservableCollection<DBFirst.ClassInfo> ClassInfoCollection { get; set; }
        private void QueryClassInfo(object sender, RoutedEventArgs e)
        {
            using (DBFirst.DbFirstEntities db = new DBFirst.DbFirstEntities())
            {
                db.Database.Connection.ConnectionString = conStr;
                QueryClassInfo(db);
            }
        }
        private void QueryClassInfo(DBFirst.DbFirstEntities db)
        {
            if (db.ClassInfo.Count() == 0) return;
            ClassInfoCollection.Clear();
            foreach (var item in db.ClassInfo) ClassInfoCollection.Add(item);
        }
        private void InsertClassInfo(object sender, RoutedEventArgs e)
        {
            if (selGrade == null) return;
            if (ClassInfoCollection.Last().Name == null) return;
            using (DBFirst.DbFirstEntities db = new DBFirst.DbFirstEntities())
            {
                db.Database.Connection.ConnectionString = conStr;
                var list = db.ClassInfo.ToList();
                if (list.Count() > 0 && list.Last().Name == ClassInfoCollection.Last(f => f.Name != null).Name) return;
                var addClass = ClassInfoCollection.Last();
                db.ClassInfo.Add(new DBFirst.ClassInfo
                {
                    Name = addClass.Name,
                    Teacher = addClass.Teacher,
                    GradeID = selGrade.GradeID
                });
                db.SaveChanges();
                QueryClassInfo(db);
            }
            MessageBox.Show("插入成功！");
        }
        private void UpdateClassInfo(object sender, RoutedEventArgs e)
        {
            if (selClass == null) return;
            using (DBFirst.DbFirstEntities db = new DBFirst.DbFirstEntities())
            {
                db.Database.Connection.ConnectionString = conStr;
                var upClass = db.ClassInfo.FirstOrDefault(f => f.ClassID == selClass.ClassID);
                if (upClass == null) return;
                upClass.Name = selClass.Name;
                upClass.Teacher = selClass.Teacher;
                if (selGrade != null) upClass.GradeID = selGrade.GradeID;
                db.SaveChanges();
                QueryClassInfo(db);
            }
            MessageBox.Show("更新成功！");
        }
        private void DeleteClassInfo(object sender, RoutedEventArgs e)
        {
            if (selClass == null) return;
            using (DBFirst.DbFirstEntities db = new DBFirst.DbFirstEntities())
            {
                db.Database.Connection.ConnectionString = conStr;
                var delClass = db.ClassInfo.FirstOrDefault(f => f.ClassID == selClass.ClassID);
                if (delClass == null) return;
                db.ClassInfo.Remove(delClass);
                db.SaveChanges();
                QueryClassInfo(db);
            }
            MessageBox.Show("删除成功！");
        }
        #endregion


        #region Stu
        private DBFirst.Stu selStu;
        public DBFirst.Stu SelStu { get { return selStu; } set { selStu = value; OnPropertyChanged("SelStu"); } }
        public ObservableCollection<DBFirst.Stu> StuInfoCollection { get; set; }
        private void QueryStuInfo(object sender, RoutedEventArgs e)
        {
            using (DBFirst.DbFirstEntities db = new DBFirst.DbFirstEntities())
            {
                db.Database.Connection.ConnectionString = conStr;
                QueryStuInfo(db);
            }
        }
        private void QueryStuInfo(DBFirst.DbFirstEntities db)
        {
            if (db.Stu.Count() == 0) return;
            StuInfoCollection.Clear();
            foreach (var item in db.Stu) StuInfoCollection.Add(item);
        }
        private void InsertStuInfo(object sender, RoutedEventArgs e)
        {
            if (selClass == null) return;
            if (StuInfoCollection.Last().Name == null) return;
            using (DBFirst.DbFirstEntities db = new DBFirst.DbFirstEntities())
            {
                db.Database.Connection.ConnectionString = conStr;
                var list = db.Stu.ToList();
                if (list.Count() > 0 && list.Last().Name == StuInfoCollection.Last(f => f.Name != null).Name) return;
                var addStu = StuInfoCollection.Last();
                db.Stu.Add(new DBFirst.Stu
                {
                    Name = addStu.Name,
                    Age = addStu.Age,
                    Sex = addStu.Sex,
                    ClassID = selClass.ClassID
                });
                db.SaveChanges();
                QueryStuInfo(db);
            }
            MessageBox.Show("插入成功！");
        }
        private void UpdateStuInfo(object sender, RoutedEventArgs e)
        {
            if (selStu == null) return;
            using (DBFirst.DbFirstEntities db = new DBFirst.DbFirstEntities())
            {
                db.Database.Connection.ConnectionString = conStr;
                var upStu = db.Stu.FirstOrDefault(f => f.Id == selStu.Id);
                if (upStu == null) return;
                upStu.Name = selStu.Name;
                upStu.Age = selStu.Age;
                upStu.Sex = SelStu.Sex;              
                if (selClass != null) upStu.ClassID = selClass.ClassID;
                db.SaveChanges();
                QueryStuInfo(db);
            }
            MessageBox.Show("更新成功！");
        }
        private void DeleteStuInfo(object sender, RoutedEventArgs e)
        {
            if (selStu == null) return;
            using (DBFirst.DbFirstEntities db = new DBFirst.DbFirstEntities())
            {
                db.Database.Connection.ConnectionString = conStr;
                var delStu = db.Stu.FirstOrDefault(f => f.Id == selStu.Id);
                if (delStu == null) return;
                db.Stu.Remove(delStu);
                db.SaveChanges();
                QueryStuInfo(db);
            }
            MessageBox.Show("删除成功！");
        }
        #endregion

    }
}
